import langList from '../config/lang'
import url from '../config/url'
import mongoose, {bookModel} from "../model";
import BookCrawler from "./bookCrawler";


export async function queryGlobalFeedBooks() {
    try {
        await bookModel.deleteMany({})
        await Promise.all((Object.keys(langList) as (keyof (typeof langList))[]).map(async (item) => {
            console.log(item + '书籍开始下载')
            const bookArr = await BookCrawler(url, langList[item])
            await bookModel.create(bookArr)
            console.log(item + '语言书籍' + '存入成功' + ',存入' + bookArr.length + '条数据')
        }))
        console.log('已全部爬取完毕')
        mongoose.disconnect()
        // console.log(bookList)
    } catch (e) {
        console.log(e)
    }
}

queryGlobalFeedBooks()

// .then(res => {
//     console.log()
// })
//            .finally(() => {
//         console.log('已全部爬取完毕')
//         mongoose.disconnect()
//     })

// async.each((Object.keys(langList) as (keyof (typeof langList))[]), (lang) => {
//     console.log(lang + '书籍开始下载')
//     BookCrawler(url, langList[lang])
// }).then(() => {
//     console.log('each')
// })

// BookCrawler(url, "fr")

